package com.minxyz.order.controller;


import com.minxyz.order.entity.Order;
import com.minxyz.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    //模拟成功或失败
    @PostMapping("/{success}")
    public String order(@PathVariable("success") Boolean success) throws Exception {
        log.info("ordering");
        Order order = new Order();
        order.setId(null);
        order.setOrderNo(String.format("%s",System.currentTimeMillis()));

        try {
            if (success) {
                orderService.saveSuccess(order);
            } else {
                orderService.saveFailure(order);
            }
        } catch (Exception e) {
            log.error("{}", e.getMessage());
            return e.getMessage();
        }
        return "调用成功";
    }

}
